package com.ffcs.crmd.tsp.admin.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import com.ctg.itrdc.platform.common.entity.PageInfo;
import com.ctg.itrdc.platform.common.utils.bean.BeanUtils;
import com.ffcs.crmd.tsp.admin.entity.JobIdentityInfo;
import com.ffcs.crmd.tsp.admin.service.IJobIdentityInfoService;
import com.ffcs.crmd.tsp.api.dto.client.TspJobIdentityInfo;
import com.ffcs.crmd.tsp.api.dto.client.TspResult;
import com.ffcs.crmd.tsp.client.core.TspClient;
import com.ffcs.crmd.tsp.common.exception.TspException;

/**
 * 
 * 功能说明:执行端注册信息service
 *
 * @author ZHONGFUHUA
 * 
 * @Date 2016年4月26日 下午5:09:12
 *
 *
 * 版本号  |   作者   |  修改时间   |   修改内容
 *
 */
@Service
public class JobIdentityInfoServiceImpl extends AbstractService implements IJobIdentityInfoService {
    
    @Override
    public void deleteJobIdentityInfo(JobIdentityInfo jobIdentityInfo) throws TspException {
        try {
            TspJobIdentityInfo tspJobIdentityInfoVO = new TspJobIdentityInfo();
            tspJobIdentityInfoVO.setClusterName(jobIdentityInfo.getClusterName());
            tspJobIdentityInfoVO.setId(jobIdentityInfo.getId());
            BeanUtils.applyIf(tspJobIdentityInfoVO, jobIdentityInfo);
            
            TspClient tspClient = getClient(jobIdentityInfo.getClusterName());
            TspResult<?> tspResult = tspClient.deleteJobIdentityInfo(tspJobIdentityInfoVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public void deleteJobIdentityInfo(List<JobIdentityInfo> jobIdentityInfoList) throws TspException {
        for(JobIdentityInfo jobIdentityInfo : jobIdentityInfoList){
            deleteJobIdentityInfo(jobIdentityInfo);
        }
    }
    
    @Override
    public JobIdentityInfo get(JobIdentityInfo jobIdentityInfo) throws TspException {
        try {
            TspJobIdentityInfo tspJobIdentityInfoVO = new TspJobIdentityInfo();
            BeanUtils.applyIf(tspJobIdentityInfoVO, jobIdentityInfo, false);
            
            TspClient tspClient = getClient(jobIdentityInfo.getClusterName());
            TspResult<TspJobIdentityInfo> tspResult = tspClient.queryJobIdentityInfo(tspJobIdentityInfoVO);
            validateResult(tspResult);
            
            JobIdentityInfo rjobIdentityInfo = new JobIdentityInfo();
            BeanUtils.applyIf(rjobIdentityInfo, tspResult.getRetVo().getObject(), false);
            return rjobIdentityInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public List<JobIdentityInfo> query(JobIdentityInfo jobIdentityInfo) throws TspException {
        try {
            TspJobIdentityInfo tspJobIdentityInfoVO = new TspJobIdentityInfo();
            BeanUtils.applyIf(tspJobIdentityInfoVO, jobIdentityInfo, false);
            
            TspClient tspClient = getClient(jobIdentityInfo.getClusterName());
            TspResult<TspJobIdentityInfo> tspResult = tspClient.queryJobIdentityInfo(tspJobIdentityInfoVO);
            validateResult(tspResult);
            
            List<TspJobIdentityInfo> tspJobIdentityInfoVOList = new ArrayList<TspJobIdentityInfo>();
            tspJobIdentityInfoVOList.addAll(tspResult.getRetVo().getDataList());
            return BeanUtils.copyList(tspJobIdentityInfoVOList, JobIdentityInfo.class);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    @Override
    public PageInfo<JobIdentityInfo> queryPage(JobIdentityInfo jobIdentityInfo, int pageNumber, int pageSize) throws TspException {
        try {
            TspJobIdentityInfo tspJobIdentityInfoVO = new TspJobIdentityInfo();
            BeanUtils.applyIf(tspJobIdentityInfoVO, jobIdentityInfo, false);
            tspJobIdentityInfoVO.setPageNumber(pageNumber);
            tspJobIdentityInfoVO.setPageSize(pageSize);
            
            TspClient tspClient = getClient(jobIdentityInfo.getClusterName());
            TspResult<TspJobIdentityInfo> tspResult = tspClient.queryJobIdentityInfo(tspJobIdentityInfoVO);
            validateResult(tspResult);
            
            com.ffcs.crmd.tsp.api.dto.page.PageInfo<TspJobIdentityInfo> tspJobIdentityInfoPageInfo = tspResult.getRetVo().getPageInfo();
            PageInfo<JobIdentityInfo> jobIdentityInfoPageInfo = new PageInfo<JobIdentityInfo>();
            BeanUtils.applyIf(jobIdentityInfoPageInfo, tspJobIdentityInfoPageInfo, false);
            jobIdentityInfoPageInfo.setList(BeanUtils.copyList(tspJobIdentityInfoPageInfo.getList(), JobIdentityInfo.class));
            return jobIdentityInfoPageInfo;
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }

    @Override
    public void saveJobIdentityInfo(JobIdentityInfo jobIdentityInfo) throws TspException {
        try {
            TspJobIdentityInfo tspJobIdentityInfoVO = new TspJobIdentityInfo();
            BeanUtils.applyIf(tspJobIdentityInfoVO, jobIdentityInfo);
            
            TspClient tspClient = getClient(jobIdentityInfo.getClusterName());
            TspResult<?> tspResult = tspClient.createJobIdentityInfo(tspJobIdentityInfoVO);
            validateResult(tspResult);
        } catch (Exception e) {
            throw new TspException(e.getMessage());
        }
    }
    
    
    @Override
    public void saveJobIdentityInfo(List<JobIdentityInfo> jobIdentityInfoList) throws TspException {
        for(JobIdentityInfo jobIdentityInfo : jobIdentityInfoList){
            saveJobIdentityInfo(jobIdentityInfo);
        }
    }
    
}
